package com.yuan.reggie.controller;

import ch.qos.logback.core.util.ContextUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.reggie.common.BaseContext;
import com.yuan.reggie.common.R;
import com.yuan.reggie.pojo.Orders;
import com.yuan.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author yuanxingke
 * @date 2022/8/4 0:01
 */
@RestController
@Slf4j
@RequestMapping("order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("page")
    public R<Page<Orders>> page(Integer page, Integer pageSize, Integer number, Date beginTime,Date endTime){

        Page<Orders> page1=new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.like(number!=null,Orders::getNumber,number)
                    .between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime);
        ordersService.page(page1,queryWrapper);

        return R.success(page1);
    }


    @GetMapping("userPage")
    public R<Page<Orders>> userPage(Integer page, Integer pageSize){

        Page<Orders> page1=new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersService.page(page1,queryWrapper);

        return R.success(page1);
    }


}
